import { reactive, ref } from 'vue'
import {
  BasicColumn
} from '/@/components/Table';
import {
  FormSchema
} from '/@/components/Table';
import {
  rules
} from '/@/utils/helper/validator';
import {
  render
} from '/@/utils/common/renderUtils';
import {
  JVxeTypes,
  JVxeColumn
} from '/@/components/jeecg/JVxeTable/types'
import { queryTreeList, getUserList } from '/@/api/common/api';
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();

const { id } = userStore.getUserInfo

const awaitList = reactive({ data: [] })
const getList = async () => {
  awaitList.data = (await getUserList({ pageNo: 1, pageSize: 9999 })).records
}
getList()
let initTreeList = ref([])
const treeList = ref([])
async function tableData() {
  initTreeList.value = await queryTreeList()
  treeList.value = flatten(initTreeList.value)
}
function flatten(arr) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    const child = arr[i].children;
    if (child?.length > 0) {
      res = res.concat(flatten(child));
    }
    res.push(arr[i]);
  }
  return res;
}
tableData()

var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
const newMonth = month < 9 ? `0${month}` : month
const newDay = day < 9 ? `0${day}` : day
const newDate = `${year}-${newMonth}-${newDay}`

//列表数据
export const columns: BasicColumn[] = [
  {
    title: '主计划编号',
    align: "center",
    sorter: true,
    dataIndex: 'mainPlanCode'
  },
  {
    title: '计划名称',
    align: "center",
    sorter: true,
    dataIndex: 'planName'
  },
  {
    title: '版本号',
    align: "center",
    dataIndex: 'version',
  },
  {
    title: '所属部门',
    align: "center",
    dataIndex: 'department',
    customRender: ({ text }) => {
      return treeList.value.filter(item => item.id == text)[0]?.departName
    }
  },
  {
    title: '计划时间',
    align: "center",
    sorter: true,
    dataIndex: 'planTime',
    customRender: ({
      text
    }) => {
      return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
    }
  },
  {
    title: '是否跑MRP',
    align: "center",
    dataIndex: 'isMrpRun',
    customRender: ({ text }) => {
      return text == 1 ? '是' : text == 0 ? '否' : ''
    },
  },
  {
    title: '是否已下发',
    align: "center",
    dataIndex: 'isIssued',
    customRender: ({ text }) => {
      return text == 1 ? '是' : text == 0 ? '否' : ''
    },
  },
  {
    title: '计划员',
    align: "center",
    dataIndex: 'planner',
    customRender: ({ text }) => awaitList.data.filter(item => text === item.id)[0]?.realname
  },
  {
    title: '计划组织',
    align: "center",
    dataIndex: 'planOrganizationName'
  },
  {
    title: '计划状态',
    align: "center",
    dataIndex: 'planStatus',
    customRender: ({ text }) => text === 0 ? '已排产' : text === 1 ? '已跑需求' : text === 2 ? '已请购' : text === 3 ? '已排产' : text === 4 ? '已完工' : text === 5 ? '已出货' : ''
  },
  {
    title: '计划周期',
    align: "center",
    dataIndex: 'planCycle'
  },
  {
    title: '执行部门',
    align: "center",
    dataIndex: 'playDept',
    customRender: ({ text }) => {
      return treeList.value.filter(item => item.id == text)[0]?.departName
    }
  },
  {
    title: '第三方编码',
    align: "center",
    dataIndex: 'thirdPartyCode',
    ifShow: false
  },
];
export const formSearchCtx: FormSchema[] = [{
  label: '主计划编号',
  field: 'mainPlanCode',
  component: 'Input',
}, {
  label: '计划名称',
  field: 'planName',
  component: 'Input',
}, {
  label: '版本号',
  field: 'version',
  component: 'Input',
}]
// 订单列表数据
export const orderColumns: BasicColumn[] = [
  //  {
  //   title: '单据类型ID',
  //   align:"center",
  //   dataIndex: 'billTypeId'
  //  },
  {
    title: '单据类型',
    align: "center",
    dataIndex: 'billTypeName'
  },
  {
    title: '订单渠道',
    align: "center",
    dataIndex: 'orderChannel',
    customRender: ({ text }) => {
      return render.renderDict(text, 'order_center_order_channel');
    },
  },
  {
    title: '单据编号',
    align: "center",
    dataIndex: 'billCode'
  },
  {
    title: '行号',
    align: "center",
    dataIndex: 'rowNum'
  },
  //  {
  //   title: '料品ID',
  //   align:"center",
  //   dataIndex: 'materialId'
  //  },
  {
    title: '料品编号',
    align: "center",
    sorter: true,
    dataIndex: 'materialCode'
  },
  {
    title: '料品名称',
    align: "center",
    dataIndex: 'materialName'
  },
  {
    title: '料品规格',
    align: "center",
    sorter: true,
    dataIndex: 'spec'
  },
  //  {
  //   title: '单位ID',
  //   align:"center",
  //   dataIndex: 'unitId',
  //  },
  {
    title: '单位名称',
    align: "center",
    dataIndex: 'unitName'
  },
  {
    title: '订单数量',
    align: "center",
    sorter: true,
    dataIndex: 'orderQuantity'
  },
  {
    title: '已交数量',
    align: "center",
    sorter: true,
    dataIndex: 'deliveryQuantity'
  },
  {
    title: '入库数量',
    align: "center",
    sorter: true,
    dataIndex: 'storageQuantity'
  },
  {
    title: '排产数量',
    align: "center",
    sorter: true,
    dataIndex: 'productQuantity'
  },
  {
    title: '关闭数量',
    align: "center",
    sorter: true,
    dataIndex: 'closeQuantity'
  },
  {
    title: '订单交期',
    align: "center",
    sorter: true,
    dataIndex: 'orderDeliveryDate',
    customRender: ({ text }) => {
      return !text ? "" : (text.length > 10 ? text.substr(0, 10) : text)
    },
  },
  {
    title: '单据状态',
    align: "center",
    dataIndex: 'billStatus',
    customRender: ({ text }) => {
      return render.renderDict(text, 'order_center_bill_status');
    },
  },
  {
    title: '和云销售订单号',
    align: "center",
    dataIndex: 'hjSerialNumber'
  },
  {
    title: '评审单号',
    align: "center",
    dataIndex: 'reviewCode'
  },
  //  {
  //   title: '业务员ID',
  //   align:"center",
  //   dataIndex: 'salesmanId'
  //  },
  {
    title: '业务员名称',
    align: "center",
    dataIndex: 'salesmanName'
  },
  //  {
  //   title: '客户ID',
  //   align:"center",
  //   dataIndex: 'customerId'
  //  },
  {
    title: '客户名称',
    align: "center",
    dataIndex: 'customerName'
  },
  //  {
  //   title: '接单组织ID',
  //   align:"center",
  //   dataIndex: 'orderOrganizationId'
  //  },
  {
    title: '接单组织名称',
    align: "center",
    dataIndex: 'orderOrganizationName'
  },
  //  {
  //   title: '生产组织ID',
  //   align:"center",
  //   dataIndex: 'produceOrganizationId'
  //  },
  {
    title: '生产组织名称',
    align: "center",
    dataIndex: 'produceOrganizationName'
  },
  {
    title: 'BOM是否完整',
    align: "center",
    dataIndex: 'bomComplete',
    customRender: ({ text }) => {
      return render.renderDict(text, 'order_center_bom_complete');
    },
  },
  {
    title: '非完整BOM是否安排生产',
    align: "center",
    dataIndex: 'notCompleteProduce',
    customRender: ({ text }) => {
      return render.renderDict(text, 'order_center_not_complete_produce');
    },
  },
  {
    title: '异步状态',
    align: "center",
    dataIndex: 'syncStatus',
    customRender: ({ text }) => {
      return render.renderDict(text, 'order_center_sync_status');
    },
  },
  {
    title: '备注',
    align: "center",
    dataIndex: 'remark'
  },
  {
    title: '第三方编码',
    align: "center",
    dataIndex: 'thirdPartyCode',
    ifShow: false
  },
];
//订单查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: "订单交期",
    field: "deliveryDate",
    component: 'DatePicker',
    componentProps: {
      showTime: true,
      valueFormat: 'YYYY-MM-DD'
    },
    colProps: {
      span: 8
    },
  },
  // {
  //   label: "截止日期",
  //   field: "",
  //   component: 'DatePicker',
  //   colProps: {
  //     span: 8
  //   },
  // },
  {
    label: "生产组织",
    field: "productionOrganization",
    component: 'JSelectDept',
    componentProps: ({ }) => {
      return {
        multiple: false,
        showButton: false,
        checkable: false,
        labelKey: 'departName'
      }
    },
    colProps: {
      span: 8
    },
  },
  {
    label: '单据类型',
    field: 'orderType',
    component: 'Input',
    slot: 'form-billType',
    colProps: {
      span: 8
    },
  },
  {
    label: "订单渠道",
    field: "orderChannel",
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: "order_center_order_channel",
      showChooseOption:false
    },
    colProps: {
      span: 8
    },
  },
  {
    label: "BOM是否完整",
    field: "bomComplete",
    component: 'Select',
    // defaultValue: 0,
    componentProps: {
      options: [{ label: '否', value: 0 }, { label: '是', value: 1 }],
      stringToNumber: true,
    }
  },
  {
    label: "订单编号",
    field: "billCode",
    component: 'Input',
    vagueSearch: true
  },
];
//表单数据
export const formSchema: FormSchema[] = [{
  label: '所属部门',
  field: 'department',
  component: 'JSelectDept',
  componentProps: ({ }) => {
    return {
      multiple: false,
      showButton: false,
      checkable: false,
    }
  },
},
{
  label: '主计划编号',
  field: 'mainPlanCode',
  component: 'Input',
  dynamicDisabled: true,
  componentProps: {
    placeholder: '后台自动生成',
  },
},
{
  label: '计划名称',
  field: 'planName',
  component: 'Input',
  dynamicRules: ({ model, schema }) => {
    return [{
      required: true,
      message: '请输入计划名称!'
    },];
  },
},
{
  label: '版本号',
  field: 'version',
  component: 'Input',
  dynamicRules: ({ model, schema }) => {
    return [{
      required: true,
      message: '请输入版本号!'
    },];
  },
},
{
  label: '计划时间',
  field: 'planTime',
  component: 'DatePicker',
  componentProps: {
    // showTime: true,
    valueFormat: 'YYYY-MM-DD'
  },
  defaultValue: newDate
},
{
  label: '是否跑MRP',
  field: 'isMrpRun',
  component: 'Select',
  defaultValue: 0,
  componentProps: {
    options: [{ label: '否', value: 0 }, { label: '是', value: 1 }],
    stringToNumber: true,
  }
},
{
  label: '是否已下发',
  field: 'isIssued',
  component: 'Select',
  defaultValue: 0,
  componentProps: {
    options: [{ label: '否', value: 0 }, { label: '是', value: 1 }],
    stringToNumber: true,
  }
},
{
  label: '计划员id',
  field: 'plannerId',
  component: 'Input',
  show: false
},
{
  label: '计划员',
  field: 'planner',
  component: 'JSelectUser',
  componentProps: {
    labelKey: 'realname',
    rowKey: 'id',
    isRadioSelection: true,
    readonly: true,
    showButton:false
  },
  defaultValue: id,
},
{
  label: '计划组织Id',
  field: 'planOrganizationId',
  component: 'Input',
  ifShow: false
},

{
  label: '计划组织',
  field: 'planOrganizationName',
  component: 'Input',
  slot: 'organizationSlot',
},
{
  label: '计划状态',
  field: 'planStatus',
  component: 'Input',
  ifShow: false
},
{
  label: '计划周期',
  field: 'planCycle',
  component: 'Input',
},
{
  label: '执行部门',
  field: 'playDept',
  component: 'JSelectDept',
  componentProps: ({ }) => {
    return {
      multiple: false,
      showButton: false,
      checkable: false
    }
  },
},
{
  label: '第三方编码',
  field: 'thirdPartyCode',
  component: 'Input',
  ifShow: false
},
// TODO 主键隐藏字段，目前写死为ID
{
  label: '',
  field: 'id',
  component: 'Input',
  show: false
},
];
//子表单数据
//子表表格配置
export const psProductionPlanMpsItemColumns: JVxeColumn[] = [
  {
    title: '行号',
    key: 'rowNum',
    type: JVxeTypes.normal,
    width: "60px",
    readonly: true
  },
  {
    title: '父类id',
    key: 'parentId',
    type: JVxeTypes.hidden,
  },
  {
    title: '订单类型Id',
    key: 'billTypeId',
    type: JVxeTypes.hidden,
  },
  {
    title: '订单类型',
    key: 'billTypeName',
    type: JVxeTypes.normal,
    width: "150px",
  },
  {
    title: '订单渠道',
    key: 'orderChannel',
    type: JVxeTypes.normal,
    width: "150px",
    placeholder: '请输入${title}',
  },
  {
    title: '订单编号',
    key: 'billCode',
    type: JVxeTypes.normal,
    width: "150px",
  },
  {
    title: '订单行号',
    key: 'orderRowNum',
    type: JVxeTypes.normal,
    width: "100px",
  },
  {
    title: '产品编码',
    key: 'materialCode',
    type: JVxeTypes.normal,
    width: "150px",
  },
  {
    title: '产品名称',
    key: 'materialName',
    type: JVxeTypes.normal,
    width: "150px",
  },
  {
    title: '单位Id',
    key: 'unitId',
    type: JVxeTypes.hidden,
    width: "200px",
  },
  {
    title: '单位',
    key: 'unitName',
    type: JVxeTypes.normal,
    width: "60px",
  },
  {
    title: '订单数量',
    key: 'orderQuantity',
    type: JVxeTypes.normal,
    disabled: true,
    width: "100px",
  },
  {
    title: '已交数量',
    key: 'deliveryQuantity',
    type: JVxeTypes.normal,
    width: "100px",
  },
  {
    title: '入库数量',
    key: 'storageQuantity',
    type: JVxeTypes.normal,
    width: "100px",
  },
  {
    title: '排产数量',
    key: 'productQuantity',
    type: JVxeTypes.inputNumber,
    width: "100px",
  },
  {
    title: '关闭数量',
    key: 'closeQuantity',
    type: JVxeTypes.normal,
    width: "100px",
  },
  {
    title: '订单交期',
    key: 'orderDeliveryDate',
    type: JVxeTypes.date,
    width: "100px",
  },
  {
    title: '业务员Id',
    key: 'salesmanId',
    type: JVxeTypes.hidden,
    width: "200px"
  },
  {
    title: '生产组织Id',
    key: 'produceOrganizationId',
    type: JVxeTypes.hidden,
    width: "200px",
  },
  {
    title: '生产组织',
    key: 'produceOrganizationName',
    type: JVxeTypes.hidden,
    width: "150px",
  },
  {
    title: 'BOM是否完整',
    key: 'bomComplete',
    type: JVxeTypes.select,
    width: "150px",
    defaultValue: 1,
    options: [{ value: 0, label: '否' }, { value: 1, label: '是' }],
  }
];

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
